package com.letv.android.client.view.channel;

import android.content.Context;
import android.graphics.PointF;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyViewPager extends ViewPager {

    /** 触摸时按下的点 **/
    private PointF downP = new PointF();
    private PointF curP = new PointF();
    private PointF moveP;
    private Context mContext;
    private boolean isIntercept = false;
    private boolean a = false;
    private Direction direction = null;
    private final String TAG = "MyViewPager";

    OnSingleTouchListener onSingleTouchListener;
    OnFlingTouchListener onFlingTouchListener;

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public MyViewPager(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        /**
         * 1.down事件首先会传递到onInterceptTouchEvent()方法
         * 2.如果onInterceptTouchEvent()在接收到down事件处理完成之return false，
         * 那么后续的move, up等事件将继续会先传递给该ViewPager，之后才和down事件一样传
         * 递给最终的目标view的onTouchEvent()处理
         * 3.如果onInterceptTouchEvent()在接收到down事件处理完成之后return true，
         * 那么后续的move, up等事件将不再传递给onInterceptTouchEvent()，而是和down
         * 事件一样传递给该ViewPager的onTouchEvent()处理，注意，目标view将接收不到任何事件
         * 相当于屏蔽了事件
         */
        if (!isIntercept) {
            if (arg0.getAction() == MotionEvent.ACTION_DOWN) {
                downP.x = arg0.getX();
                downP.y = arg0.getY();
            } else if (arg0.getAction() == MotionEvent.ACTION_UP) {
                curP.x = arg0.getX();
                curP.y = arg0.getY();
            } else if (arg0.getAction() == MotionEvent.ACTION_MOVE) {
                curP.x = arg0.getX();
                curP.y = arg0.getY();
                // if(downP.y - curP.y>5){
                if (onFlingTouchListener != null) {
                    onFlingTouchListener.onFlingDownToUpTouch();
                    // LogInfo.log("xxxx", "-------------->onFlingDownToUpTouch");
                }
                // }
            }
            return super.onInterceptTouchEvent(arg0);
        } else {
            return true;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        // 记录每次onTouchEvent的坐标值
        curP.x = arg0.getX();
        curP.y = arg0.getY();

        if (isIntercept) {
            // 屏蔽Touch事件
            return true;
        }

        switch (arg0.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
                if (downP.x == curP.x && downP.y == curP.y) {
                    if (onSingleTouchListener != null) {
                        onSingleTouchListener.onSingleTouch();
                    }
                } else if (downP.x - curP.x < -100 && getCurrentItem() == 0) {
                    if (onFlingTouchListener != null) {
                        // onFlingTouchListener.onFlingLeftToRightTouch();
                    }
                } else if (downP.x - curP.x > 100 && getCurrentItem() == (getChildCount() - 1)) {
                    if (onFlingTouchListener != null) {
                        // onFlingTouchListener.onFlingRightToLeftTouch();
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (downP.x > arg0.getX()) {
                    // 向左滑动
                    direction = Direction.LEFT;
                } else if (downP.x < arg0.getX()) {
                    // 向右滑动
                    direction = Direction.RIGHT;
                }

                break;
            default:
                break;
        }

        return super.onTouchEvent(arg0);
    }

    public interface OnSingleTouchListener {

        void onSingleTouch();
    }

    public interface OnFlingTouchListener {

        void onFlingLeftToRightTouch(); // 向右侧滑动

        void onFlingRightToLeftTouch(); // 向左滑动

        void onFlingDownToUpTouch();
    }

    public enum Direction {
        LEFT(0), RIGHT(1);

        Direction(int val) {
        }
    }

    public void setOnSingleTouchListener(OnSingleTouchListener onSingleTouchListener) {
        this.onSingleTouchListener = onSingleTouchListener;
    }

    public void setOnFlingTouchListener(OnFlingTouchListener onFlingTouchListener) {
        this.onFlingTouchListener = onFlingTouchListener;
    }

    /**
     * 设置是否需要屏蔽用户的onTouch事件
     * 
     * @param isIntercept
     */
    public void setIntercept(boolean isIntercept) {
        this.isIntercept = isIntercept;
    }

    public void setUnScroll(boolean isIntercept) {
        this.isIntercept = isIntercept;
    }

    public Direction getDirection() {
        return direction;
    }

    public void clearDirection() {
        this.direction = null;
    }

    @Override
    protected boolean canScroll(View arg0, boolean arg1, int arg2, int arg3, int arg4) {
        return super.canScroll(arg0, arg1, arg2, arg3, arg4);
    }
}
